package cn.smile.service.impl;

import cn.smile.annotation.PermissionName;
import cn.smile.dao.PermissionDaoMapper;
import cn.smile.pojo.Permission;
import cn.smile.pojo.User;
import cn.smile.service.PermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MaoSuyu
 * @User：John
 * @Date: 2019/7/4
 * @Time: 17:46
 * @Description: No Description
 */
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDaoMapper permissionDaoMapper;
    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;


    @Override
    public boolean addPermission(Permission permission) {
        return permissionDaoMapper.insert(permission) > 0 ? true : false;
    }

    @Override
    public boolean addPermission(List<Permission> permissions) {
        boolean flag = false;
        for (int i = 0; i < permissions.size(); i++) {
            flag = permissionDaoMapper.insert(permissions.get(i)) > 0 ? true : false;
        }
        return flag;
    }

    @Override
    public void refreshPermission() {
        //得到controller中带有@RequestMapping的方法
        final Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        //得到数据库中所有权限表达式
        List<String> p = permissionDaoMapper.selectPermission();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> result : handlerMethods.entrySet()) {
            //得到具体的方法
            final HandlerMethod handlerMethod = result.getValue();
            //得到注解为@RequiresPermissions的对象
            final RequiresPermissions requiresPermissions = handlerMethod.getMethodAnnotation(RequiresPermissions.class);
            if (null != requiresPermissions) {
                //得到具体的权限表达式，如：USER:REFRESH
                String permission = requiresPermissions.value()[0];
                if (p.contains(permission)) {
                    //如果数据库中包含其权限表达式，则什么都不做
                    continue;
                }
                //用于得到权限的名字
                final PermissionName methodAnnotation = handlerMethod.getMethodAnnotation(PermissionName.class);
                //权限叫什么的名字
                final String permissionName = methodAnnotation.permissionName();
                //对权限的具体信息进行包装
                Permission per = new Permission(permission, permissionName);
                final int b = permissionDaoMapper.insert(per);
                System.out.println("权限刷新状态：" + b);
            }
        }
    }

    @Override
    public List<String> findPermissionById(User user) {
        return permissionDaoMapper.selectPermissionById(user);
    }

}
